import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_app/utils/channel.dart';

class PlatformChannel extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _PlatformChannel();
  }
}

class _PlatformChannel extends State<PlatformChannel> {
  var netChangeStr = "点我获取当前网络状态";

  @override
  void initState() {
    super.initState();
    Channel.eventChannel
        .receiveBroadcastStream()
        .listen(_onEvent, onError: _onError);
  }

  _onEvent(Object event) {
    setState(() {
      netChangeStr = event;
    });
  }

  _onError(Object error) {
    setState(() {
      netChangeStr = "网络状态获取失败";
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("PlatformChannel"),
        centerTitle: true,
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            RaisedButton(
              child: Text("点我提示"),
              onPressed: () {
                Channel.showToast("我是android系统的toast");
              },
            ),
            RaisedButton(
              child: Text("点我获取Android平台数据"),
              onPressed: () {
                Channel.getAndroidTime().then((time) {
                  Channel.showToast(time);
                });
              },
            ),
            RaisedButton(
              child: Text(netChangeStr),
              onPressed: () {
                Channel.isConnection().then((isConnection) {
                  Channel.showToast(isConnection ? "网络连接成功" : "网络连接失败");
                });
              },
            )
          ],
        ),
      ),
    );
  }
}
